const httpUrl = getApp().globalData.httpUrl;

const hideLoading = () =>{
  return new Promise((resolve, reject) => {
    wx.hideLoading({
      success: () =>{
        resolve(true); // 成功关闭loading，返回true
      },
      fail: () =>{
        reject(false); // 关闭loading失败，返回false
      }
    });
  });
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money:"请认真发表您的评价吧~",
    content:"",
    length:0,
    image:null,
    shopId:'',
    orderId:'',
    value:5,
  },
  onChange(event) {
    this.setData({
      value: event.detail,
    });
  },

  setText(e){
    this.setData({
        content:e.detail.value,
        length:e.detail.value.length
    })
  },
  cleanValue(){
    this.setData({
        content:"",
        length:0,
        image:null,

    })
  },
  cloudSubmit(){
    var that = this;
    var date = new Date().getTime();
    wx.request({
      url: httpUrl + '/comment' ,
      method:'POST',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      data:{
        shopId:that.data.shopId,
        orderId:that.data.orderId,
        date:date,
        score:that.data.value,
        image:that.data.image,
        comment:that.data.content
      },
      success(res){
        if (res.data.code == 20011) {
          wx.showToast({
            title: res.data.msg,
          })
          setTimeout(()=>{
            wx.navigateBack({
              delta:1
            })
          }, 1500);
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else if (res.data.code == 402) {
          console.log(res.data.msg);
          // wx.removeStorageSync('Shop_token');
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
        else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
  },
  afterRead(event) {
    var that = this;
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: httpUrl + '/file/upload', // 仅为示例，非真实的接口地址
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      filePath: file.url,
      name: 'file',
      formData: { user: 'test' },
      success(res) {
        if (res.data == "") {
          wx.showToast({
            title: '图片尺寸太大',
            icon:'error'
          })
          return;
        }
        var json = JSON.parse(res.data);
        if (json.code == 30011) {
          that.setData({
            image:json.data
          },()=>{
            wx.showToast({
              title: '图片上传成功',
  
            })
          })
        }
      },
      fail(err){
        console.log(err);
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      shopId:options.shopId,
      orderId:options.order
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})